//
//  CrudeOilExchangeList.m
//  DataCollector
//
//  Created by jiro on 12/06/11.
//  Copyright 2012 jiro music. All rights reserved.
//

#import "CrudeOilExchangeList.h"

#import "CrudeOilExchange.h"

@implementation CrudeOilExchangeList

+ (id) crudeOilExchangeListWithPgResult: (PGresult*) res
{
	CrudeOilExchangeList* ret = [ [ CrudeOilExchangeList alloc ] init ];
	int rowCount = PQntuples( res );
	int columnCount = PQnfields( res );
	for ( int i = 0; i < rowCount; i++ )
	{
		NSMutableArray* values = [ [ NSMutableArray alloc ] init ];
		for ( int j = 0; j < columnCount; j++ )
		{
			[ values addObject: [ NSString stringWithCString: PQgetvalue( res, i, j ) encoding: NSUTF8StringEncoding ] ];
		}
		[ ret addObject: [ CrudeOilExchange crudeOilExchangeWithValues: values ] ];
	}
	return ret;

}

- (NSString*) sqlInsertStatement
{
	NSString* s = [ NSString stringWithFormat: @"insert into %@ values ", [ CrudeOilExchange tableName ] ];
	for ( int i = 0, count = [ self count ]; i < count; i++ )
	{
		CrudeOilExchange* code = [ self objectAtIndex: i ];
		if ( i != count - 1 )
		{	// add ',' 
			s = [ s stringByAppendingFormat: @"%@, ", [ code queryInsertValue ] ];
		}
		else
		{	//	';'
			s = [ s stringByAppendingFormat: @"%@;", [ code queryInsertValue ] ];
		}
	}
	return s;
}

@end
